
import 'package:flutter/material.dart';
import 'package:xsdk_flutter/xsdk/xsdk_flutter_package.dart';


/*
自定义键盘输入(数字,横屏)
 */
class XPageStateCustomKeyboardNumberInputLandscape extends XPageState{

  String? initValue;
  Color? btSubmitColor;//确认键颜色

  String _currentResult='';
  XPageStateCustomKeyboardNumberInputLandscape(
      {
        this.initValue, //数字
        this.btSubmitColor,
      }
      );

  @override
  void onCreate() {
    super.onCreate();
    if(widget.xTempValue1==null){
      //XLogUtils.printLog('---init widget.xTempValue1');
      widget.xTempValue1=[];
      if(!XStringUtils.isEmpty(initValue)){
        (widget.xTempValue1 as List).addAll(XStringUtils.string2List(initValue));
      }
    }
  }

  @override
  Widget getBodyWidget(BuildContext context) {
    return XWidgetUtils.getLayoutContainer(
        alignment: Alignment.center,
        child: XWidgetUtils.getXLayoutRoundedRect(
            backgroundColor: Colors.white,
            width:676,
            height:470,
            child: XWidgetUtils.getLayoutColumn(
                children: [
                  _getTopWidget(),
                  _getbottomWidget(),
                ]
            )
        )
    );
  }

  //下半部分
  Widget _getbottomWidget(){
    return XWidgetUtils.getLayoutExpanded(
        XWidgetUtils.getXLayoutRoundedRectWithBorder(
            radius_bottomLeft: 10,
            radius_bottomRight: 10,
            radius_topRight: 0,
            radius_topLeft: 0,
            width: double.infinity,
            borderWidth: 0,
            backgroundColor: XColorUtils.pageBackgroundColor,
            padding: XWidgetUtils.getEdgeInsets(24, 24, 24, 24),
            child: XWidgetUtils.getLayoutRow(
                children: [
                  //左边数字widget
                  _getWidgetLeftNumbers(),
                  //右边按钮widget
                  _getWidgetRightButtoms(),
                ]
            ),
        )
    );
  }

  //上半部分Widget
  Widget _getTopWidget(){
    return XWidgetUtils.getXLayoutRoundedRectWithBorder(
        radius_bottomLeft: 0,
        radius_bottomRight: 0,
        radius_topRight: 10,
        radius_topLeft: 10,
        backgroundColor: Colors.white,
        borderWidth: 0,
        child: XWidgetUtils.getLayoutColumn(
            children: [
              _getWidgetTitleBar(),
              XWidgetUtils.getWidgetDivider(marginRight: 0,marginLeft: 0),
              _getWidgetInputResult(),
            ]
        )
    );
  }


  //结果内容
  Widget _getWidgetInputResult(){
    return XWidgetUtils.getLayoutContainer(
        height: 119,
        alignment: Alignment.centerLeft,
        backgroundColor: Colors.white,
        child: XWidgetUtils.getWidgetText(
            _currentResult,
            style:XStyleUtils.getTextStyle(
                fontSize: 22,
                color: Colors.black
            ),
            padding: XWidgetUtils.getEdgeInsets(24, 0, 24, 0)
        )
    );
  }

  //标题栏
  Widget _getWidgetTitleBar(){
    return XWidgetUtils.getLayoutContainer(
        height: 48,
        child: XWidgetUtils.getLayoutRow(
            children: [
              XWidgetUtils.getLayoutExpanded(XWidgetUtils.getWidgetText(
                  '修改数量',
                  style: XStyleUtils.textStyle_000_16(),
                  padding: XWidgetUtils.getEdgeInsets(20, 0, 0, 0)
              )),
              XWidgetUtils.getWidgetGestureDetector(
                  child: XWidgetUtils.getLayoutContainer(
                    backgroundColor: Colors.white,
                      width: 100,
                      margin: XWidgetUtils.getEdgeInsets(0, 0, 10, 0),
                      padding: XWidgetUtils.getEdgeInsets(0, 0, 5, 0),
                      alignment: Alignment.centerRight,
                      child: XImageUtils.getImageWidgetWithAsset(
                          'x_quxiao.png',
                          width: 20,
                          height: 20
                      )
                  ),
                  onTap: (){
                    finishPage();
                  }
              ),
            ]
        )
    );
  }

  @override
  Color getXPageBackgroundColor() {
    return Colors.black54;
  }
  @override
  void onStart() {
  }

  @override
  Future<void> updateData({int pageIndex = 1}) async {
  }

  ////////////////////////////////////////////////////////////////

  //按键输入
  void _pressKeyInput(String key){

    //XLogUtils.printLog('---key=$key,${XStringUtils.list2String(widget.xTempValue1)}');

    if(key=='确定'){
      finishPage(result: XNumberUtils.string2double(XStringUtils.list2String(widget.xTempValue1)));
      return;
    }else if(key=='删除'){
      if((widget.xTempValue1 as List).isEmpty){
        return;
      }
      (widget.xTempValue1 as List).removeLast();
    }else if(key=='点'){
      if((widget.xTempValue1 as List).isEmpty){
        return;
      }
      if((widget.xTempValue1 as List).contains('.')){
        return;
      }
      (widget.xTempValue1 as List).add('.');
    }else if(key=='00'){
      if((widget.xTempValue1 as List).isEmpty){
        return;
      }
      if((widget.xTempValue1 as List).length==1 && (widget.xTempValue1 as List)[0]=='0'){
        return;
      }
      if(!_isCanInputNumber(is00Key:true)){
        return;
      }

      (widget.xTempValue1 as List).add('0');
      (widget.xTempValue1 as List).add('0');

    }else if(key=='0'){
      if((widget.xTempValue1 as List).length==1 && (widget.xTempValue1 as List)[0]=='0'){
        return;
      }
      if(!_isCanInputNumber()){
        return;
      }
      (widget.xTempValue1 as List).add(key);

    }else{
      //1~9
      if((widget.xTempValue1 as List).length==1 && (widget.xTempValue1 as List)[0]=='0'){
        (widget.xTempValue1 as List).clear();
      }

      if(!_isCanInputNumber()){
        return;
      }
      (widget.xTempValue1 as List).add(key);
    }

    //XLogUtils.printLog('---c= ${XStringUtils.list2String(widget.xTempValue1)}');

    setState(() {
      _currentResult=XStringUtils.list2String(widget.xTempValue1??[]);
    });

  }

  //是否限制,有效位数
  bool _isCanInputNumber({bool is00Key=false}){
    int length=(widget.xTempValue1 as List).length;

    if((widget.xTempValue1 as List).contains('.')){
      //小数限制3位
      int limitXiaoshu=3;
      int index=0;
      for(int i=0;i<length;i++){
        if((widget.xTempValue1 as List)[i]=='.'){
          index=i;
          break;
        }
      }

      if(length-index>=(is00Key?limitXiaoshu:limitXiaoshu+1)){
        //当前已达到3位小数,不能再添加
        toast('最大输入数限制$limitXiaoshu位小数');
        return false;
      }

    }else{
      //整数限制8位
      int limitZhengshu=8;
      if(length>=(is00Key?limitZhengshu-1:limitZhengshu)){
        toast('最大输入数限制$limitZhengshu位整数');
        return false;
      }

    }

    return true;
  }


  //左边数字widget
  Widget _getWidgetLeftNumbers(){
    return XWidgetUtils.getLayoutExpanded(
        XWidgetUtils.getLayoutColumn(
            children: [
              XWidgetUtils.getLayoutRow(
                  children: [
                    XWidgetUtils.getLayoutExpanded(
                        XWidgetUtils.getWidgetGestureDetector(
                            child: XWidgetUtils.getXLayoutRoundedRectWithBorder(
                                borderColor:XColorUtils.colorCCCCCC,
                                height: 57,
                                alignment: Alignment.center,
                                child: XWidgetUtils.getWidgetText(
                                    '1',
                                    style: XStyleUtils.getTextStyle(
                                        fontSize: 20,
                                        color: Colors.black
                                    )
                                )
                            ),
                            onTap: (){
                              _pressKeyInput('1');
                            }
                        )
                    ),
                    XWidgetUtils.getLayoutContainer(width: 8),
                    XWidgetUtils.getLayoutExpanded(
                        XWidgetUtils.getWidgetGestureDetector(
                            child: XWidgetUtils.getXLayoutRoundedRectWithBorder(
                                borderColor:XColorUtils.colorCCCCCC,
                                height: 57,
                                alignment: Alignment.center,
                                child: XWidgetUtils.getWidgetText(
                                    '2',
                                    style: XStyleUtils.getTextStyle(
                                        fontSize: 20,
                                        color: Colors.black
                                    )
                                )
                            ),
                            onTap: (){
                              _pressKeyInput('2');
                            }
                        )
                    ),
                    XWidgetUtils.getLayoutContainer(width: 8),
                    XWidgetUtils.getLayoutExpanded(
                        XWidgetUtils.getWidgetGestureDetector(
                            child: XWidgetUtils.getXLayoutRoundedRectWithBorder(
                                borderColor:XColorUtils.colorCCCCCC,
                                height: 57,
                                alignment: Alignment.center,
                                child: XWidgetUtils.getWidgetText(
                                    '3',
                                    style: XStyleUtils.getTextStyle(
                                        fontSize: 20,
                                        color: Colors.black
                                    )
                                )
                            ),
                            onTap: (){
                              _pressKeyInput('3');
                            }
                        )
                    ),
                  ]
              ),
              XWidgetUtils.getLayoutContainer(height: 8),
              XWidgetUtils.getLayoutRow(
                  children: [
                    XWidgetUtils.getLayoutExpanded(
                        XWidgetUtils.getWidgetGestureDetector(
                            child: XWidgetUtils.getXLayoutRoundedRectWithBorder(
                                borderColor:XColorUtils.colorCCCCCC,
                                height: 57,
                                alignment: Alignment.center,
                                child: XWidgetUtils.getWidgetText(
                                    '4',
                                    style: XStyleUtils.getTextStyle(
                                        fontSize: 20,
                                        color: Colors.black
                                    )
                                )
                            ),
                            onTap: (){
                              _pressKeyInput('4');
                            }
                        )
                    ),
                    XWidgetUtils.getLayoutContainer(width: 8),
                    XWidgetUtils.getLayoutExpanded(
                        XWidgetUtils.getWidgetGestureDetector(
                            child: XWidgetUtils.getXLayoutRoundedRectWithBorder(
                                borderColor:XColorUtils.colorCCCCCC,
                                height: 57,
                                alignment: Alignment.center,
                                child: XWidgetUtils.getWidgetText(
                                    '5',
                                    style: XStyleUtils.getTextStyle(
                                        fontSize: 20,
                                        color: Colors.black
                                    )
                                )
                            ),
                            onTap: (){
                              _pressKeyInput('5');
                            }
                        )
                    ),
                    XWidgetUtils.getLayoutContainer(width: 8),
                    XWidgetUtils.getLayoutExpanded(
                        XWidgetUtils.getWidgetGestureDetector(
                            child: XWidgetUtils.getXLayoutRoundedRectWithBorder(
                                borderColor:XColorUtils.colorCCCCCC,
                                height: 57,
                                alignment: Alignment.center,
                                child: XWidgetUtils.getWidgetText(
                                    '6',
                                    style: XStyleUtils.getTextStyle(
                                        fontSize: 20,
                                        color: Colors.black
                                    )
                                )
                            ),
                            onTap: (){
                              _pressKeyInput('6');
                            }
                        )
                    ),
                  ]
              ),
              XWidgetUtils.getLayoutContainer(height: 8),
              XWidgetUtils.getLayoutRow(
                  children: [
                    XWidgetUtils.getLayoutExpanded(
                        XWidgetUtils.getWidgetGestureDetector(
                            child: XWidgetUtils.getXLayoutRoundedRectWithBorder(
                                borderColor:XColorUtils.colorCCCCCC,
                                height: 57,
                                alignment: Alignment.center,
                                child: XWidgetUtils.getWidgetText(
                                    '7',
                                    style: XStyleUtils.getTextStyle(
                                        fontSize: 20,
                                        color: Colors.black
                                    )
                                )
                            ),
                            onTap: (){
                              _pressKeyInput('7');
                            }
                        )
                    ),
                    XWidgetUtils.getLayoutContainer(width: 8),
                    XWidgetUtils.getLayoutExpanded(
                        XWidgetUtils.getWidgetGestureDetector(
                            child: XWidgetUtils.getXLayoutRoundedRectWithBorder(
                                borderColor:XColorUtils.colorCCCCCC,
                                height: 57,
                                alignment: Alignment.center,
                                child: XWidgetUtils.getWidgetText(
                                    '8',
                                    style: XStyleUtils.getTextStyle(
                                        fontSize: 20,
                                        color: Colors.black
                                    )
                                )
                            ),
                            onTap: (){
                              _pressKeyInput('8');
                            }
                        )
                    ),
                    XWidgetUtils.getLayoutContainer(width: 8),
                    XWidgetUtils.getLayoutExpanded(
                        XWidgetUtils.getWidgetGestureDetector(
                            child: XWidgetUtils.getXLayoutRoundedRectWithBorder(
                                borderColor:XColorUtils.colorCCCCCC,
                                height: 57,
                                alignment: Alignment.center,
                                child: XWidgetUtils.getWidgetText(
                                    '9',
                                    style: XStyleUtils.getTextStyle(
                                        fontSize: 20,
                                        color: Colors.black
                                    )
                                )
                            ),
                            onTap: (){
                              _pressKeyInput('9');
                            }
                        )
                    ),
                  ]
              ),
              XWidgetUtils.getLayoutContainer(height: 8),
              XWidgetUtils.getLayoutRow(
                  children: [
                    XWidgetUtils.getLayoutExpanded(
                        XWidgetUtils.getWidgetGestureDetector(
                            child: XWidgetUtils.getXLayoutRoundedRectWithBorder(
                                borderColor:XColorUtils.colorCCCCCC,
                                height: 57,
                                alignment: Alignment.center,
                                child: XWidgetUtils.getWidgetText(
                                    '0',
                                    style: XStyleUtils.getTextStyle(
                                        fontSize: 20,
                                        color: Colors.black
                                    )
                                )
                            ),
                            onTap: (){
                              _pressKeyInput('0');
                            }
                        )
                    ),
                    XWidgetUtils.getLayoutContainer(width: 8),
                    XWidgetUtils.getLayoutExpanded(
                        XWidgetUtils.getWidgetGestureDetector(
                            child: XWidgetUtils.getXLayoutRoundedRectWithBorder(
                                borderColor:XColorUtils.colorCCCCCC,
                                height: 57,
                                alignment: Alignment.center,
                                child: XWidgetUtils.getWidgetText(
                                    '00',
                                    style: XStyleUtils.getTextStyle(
                                        fontSize: 20,
                                        color: Colors.black
                                    )
                                )
                            ),
                            onTap: (){
                              _pressKeyInput('00');
                            }
                        )
                    ),
                    XWidgetUtils.getLayoutContainer(width: 8),
                    XWidgetUtils.getLayoutExpanded(
                        XWidgetUtils.getWidgetGestureDetector(
                            child: XWidgetUtils.getXLayoutRoundedRectWithBorder(
                                borderColor:XColorUtils.colorCCCCCC,
                                height: 57,
                                alignment: Alignment.center,
                                child: XWidgetUtils.getWidgetText(
                                    '•',
                                    style: XStyleUtils.getTextStyle(
                                        fontSize: 20,
                                        color: Colors.black
                                    )
                                )
                            ),
                            onTap: (){
                              _pressKeyInput('点');
                            }
                        )
                    ),
                  ]
              ),
            ]
        )
    );
  }
  //右边按钮widget
  Widget _getWidgetRightButtoms(){

    return XWidgetUtils.getLayoutContainer(
        width: 100,
        margin: XWidgetUtils.getEdgeInsets(8, 0, 0, 0),
        child: XWidgetUtils.getLayoutColumn(
            children: [
              XWidgetUtils.getWidgetGestureDetector(
                  child: XWidgetUtils.getXLayoutRoundedRectWithBorder(
                      borderColor:XColorUtils.colorCCCCCC,
                      height: 57,
                      alignment: Alignment.center,
                      child: XWidgetUtils.getWidgetText(
                          '←',
                          style: XStyleUtils.getTextStyle(
                              fontSize: 20,
                              color: Colors.black
                          )
                      )
                  ),
                  onTap: (){
                    _pressKeyInput('删除');
                  }
              ),
              XWidgetUtils.getLayoutContainer(height: 8),
              XWidgetUtils.getWidgetGestureDetector(
                  child: XWidgetUtils.getXLayoutRoundedRectWithBorder(
                      height: 187,
                      borderColor:btSubmitColor??XColorUtils.appMainColor,
                      backgroundColor: btSubmitColor??XColorUtils.appMainColor,
                      alignment: Alignment.center,
                      child: XWidgetUtils.getWidgetText(
                          '确定',
                          style: XStyleUtils.getTextStyle(
                              fontSize: 20,
                              color: Colors.white
                          )
                      )
                  ),
                  onTap: (){
                    _pressKeyInput('确定');
                  }
              ),
            ]
        )
    );
  }

}